﻿using System;
using System.Globalization;

namespace Eflong.Structure
{
    public static class StringExt
    {
        public static String bSubstring( this string s , int length )
        {
            byte[ ] bytes = System.Text.Encoding.Unicode.GetBytes( s );
            int n = 0;  //  表示当前的字节数
            int i = 0;  //  要截取的字节数
            for ( ; i < bytes.GetLength( 0 ) && n < length; i++ )
            {
                //  偶数位置，如0、2、4等，为UCS2编码中两个字节的第一个字节
                if ( i % 2 == 0 )
                {
                    n++;      //  在UCS2第一个字节时n加1
                }
                else
                {
                    //  当UCS2编码的第二个字节大于0时，该UCS2字符为汉字，一个汉字算两个字节
                    if ( bytes[ i ] > 0 )
                    {
                        n++;
                    }
                }
            }

            //  如果i为奇数时，处理成偶数
            if ( i % 2 == 1 )
            {
                //  该UCS2字符是汉字时，去掉这个截一半的汉字
                if ( bytes[ i ] > 0 )

                    i = i - 1;

                 //  该UCS2字符是字母或数字，则保留该字符
                else
                    i = i + 1;
            }
            return System.Text.Encoding.Unicode.GetString( bytes , 0 , i );
        }
    }

    public static class Common
    {
        public static uint GetUIntFromByteArray( byte[ ] bs , ref int pos )
        {
            uint u = 0;
            u = ( ( uint )bs[ pos ] ) | ( uint )( bs[ pos + 1 ] << 8 ) | ( uint )( bs[ pos + 2 ] << 16 ) | ( uint )( bs[ pos + 3 ] << 24 );
            pos += 4;
            return u;
        }

        public static string GetStringFrom( float f )
        {
            return String.Format( "{0:F2}" , f );
        }

        public static string GetDateTimeString( DateTime time )
        {
            return time.ToString( "dd/HH:mm:ss" , DateTimeFormatInfo.InvariantInfo );
        }

        /// -500水平
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string GetChineseNumberString( string s )
        {
            char[ ] strNum = s.ToCharArray( );

            string result = string.Empty;

            for ( int n = 0; n < s.Length; n++ )
            {
                char c = s[ n ];
                if ( c >= '0' && c <= '9' )
                {
                    result += GetValue( s[ n ].ToString( ).ToCharArray( )[ 0 ] );
                }
                else if ( c == '-' )
                {
                    result += "负";
                }
                else
                {
                    result += s[ n ];
                }
            }

            return result;
        }

        private static string GetChineseIntegerNumString( string s )
        {
            char[ ] strNum = s.ToCharArray( );

            int high = strNum.Length - 1;
            int plus = 0;
            char zero = '0';

            for ( int i = high; i >= 0; i-- )
            {
                if ( plus == 4 )
                    plus = 0;

                if ( zero == '0' && strNum[ i ] == '0' )
                    strNum[ i ] = '-';
                else
                    zero = strNum[ i ];

                if ( i < ( i + plus ) && i < ( high - 3 ) && strNum[ i ] != '-' && strNum[ i + plus ] == '-' )
                    strNum[ i + plus ] = '+';

                if ( plus == 1 && strNum[ i ] == '1' && i == 0 )
                    strNum[ i ] = '+';

                plus++;
            }

            if ( strNum.Length == 1 && strNum[ 0 ] == '-' )
                strNum[ 0 ] = '0';

            string chsNum = string.Empty;
            int unit = 0;

            for ( int j = high; j >= 0; j-- )
            {
                if ( strNum[ j ] != '-' )
                {
                    if ( strNum[ j ] == '+' )
                        chsNum = GetUnit( unit ) + chsNum;
                    else if ( strNum[ j ] == '0' )
                        chsNum = GetValue( strNum[ j ] ) + chsNum;
                    else
                        chsNum = GetValue( strNum[ j ] ) + GetUnit( unit ) + chsNum;
                }
                unit++;
            }
            return chsNum;
        }

        private static string GetChineseDecimalString( string s )
        {
            char[ ] strNum = s.ToCharArray( );

            int high = strNum.Length - 1;

            string chsNum = string.Empty;
            int unit = 0;

            for ( int j = high; j >= 0; j-- )
            {
                chsNum = GetValue( strNum[ j ] ) + chsNum;
                unit++;
            }
            return chsNum;
        }

        public static string GetChineseStringFrom( float f , string sUnit )
        {
            string s = string.Empty;
            if ( sUnit.IndexOf( "%" ) >= 0 )
            {
                s = "百分之" + GetChineseStringForFloat( f , 2 );
            }
            else
            {
                s = GetChineseStringForFloat( f , 1 );

                switch ( sUnit.ToLower( ) )
                {
                    case "m/s":
                        s += "米每秒";
                        break;

                    case "kpa":
                        s += "千帕";
                        break;

                    case "℃":
                        s += "度";
                        break;

                    case "mg/m3":
                        s += "每立方毫克";
                        break;

                    case "m":
                        s += "米";
                        break;

                    case "ppm":
                        s += "ppm";
                        break;
                    default:
                        s += sUnit;
                        break;
                }
            }
            return s;
        }

        private static string GetValue( char c )
        {
            switch ( c )
            {
                case '0':
                    return "零";
                case '1':
                    return "一";
                case '2':
                    return "二";
                case '3':
                    return "三";
                case '4':
                    return "四";
                case '5':
                    return "五";
                case '6':
                    return "六";
                case '7':
                    return "七";
                case '8':
                    return "八";
                case '9':
                    return "九";
                case '.':
                    return "点";
            }
            return null;
        }

        private static string GetUnit( int i )
        {
            switch ( i )
            {
                case 0:
                    return "";
                case 1:
                    return "十";
                case 2:
                    return "百";
                case 3:
                    return "千";
                case 4:
                    return "万";
                case 5:
                    return "十";
                case 6:
                    return "百";
                case 7:
                    return "千";
                case 8:
                    return "亿";
                case 9:
                    return "十";
                case 10:
                    return "百";
                case 11:
                    return "千";
                case 12:
                    return "万";
            }
            return null;
        }

        public static string GetChineseStringForFloat( float f , int d )
        {
            string s = ( d == 2 ) ? String.Format( "{0:F2}" , f ) : String.Format( "{0:F1}" , f );
            string[ ] ss = s.Split( '.' );
            string chsNum = GetChineseIntegerNumString( ss[ 0 ] );
            if ( ss.Length == 2 )
            {
                string chsNum0 = GetChineseDecimalString( ss[ 1 ] );
                return chsNum + "点" + chsNum0;
            }
            else
            {
                return chsNum;
            }
        }
    }
}